﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using VisionCloud.Core.Model.Ms;
using VisionCloud.Domain.DtoModel.Ms;
using VisionCloud.Domain.Repository.Interface.Ms;

namespace VisionCloud.Domain.Repository.Implement.Ms
{
    public class MsPointTypeMeasureItemRepository : BaseRepository<MsPointTypeMeasureItem>, IMsPointTypeMeasureItemRepository
    {
        public async Task<List<MsPointTypeMeasureItem>> GetList(MsPointTypeMeasureItemCondition condition)
        {
            return await (
                from h in Db.MsPointTypeMeasureItems
                where h.ProjectStandardId == condition.ProjectStandardId && h.ProjectGuid == condition.ProjectGuid
                select h).AsNoTracking().ToListAsync().ConfigureAwait(false);
        }

        public async Task<List<MsPointTypeMeasureItem>> GetTemplateList()
        {
            return await (
                from h in Db.MsPointTypeMeasureItems
                where h.ProjectGuid == new Guid()
                select h).AsNoTracking().ToListAsync().ConfigureAwait(false);
        }

        public async Task<List<MsPointTypeMeasureItem>> GetDataByProjects(Guid[] projectIds)
        {
            return await (
               from h in Db.MsPointTypeMeasureItems
               where projectIds.Contains(h.ProjectGuid)
               select h).AsNoTracking().ToListAsync().ConfigureAwait(false);
        }

        public async Task<List<MsPointTypeMeasureItem>> GetListAsNoTracking(Expression<Func<MsPointTypeMeasureItem, bool>> expression = null)
        {
            return await Db.MsPointTypeMeasureItems.Where(expression).AsNoTracking().ToListAsync();
        }

        /// <summary>
        /// 根据项目阶段Id过滤
        /// </summary>
        /// <param name="projectIds">项目Id</param>
        /// <param name="excludeStandard">过滤项目阶段Id</param>
        /// <returns></returns>
        public async Task<List<MsPointTypeMeasureItem>> GetDataByProjectsWithStandardId(Guid[] projectIds, Guid excludeStandard)
        {
            return await (
               from h in Db.MsPointTypeMeasureItems
               where projectIds.Contains(h.ProjectGuid) && (h.ProjectStandardId != excludeStandard || excludeStandard == Guid.Empty)
               select h).AsNoTracking().ToListAsync().ConfigureAwait(false);
        }

        public async Task<List<MsPointTypeMeasureItem>> GetDataByProjectsAndPointType(Guid projectId, Guid projectStandardId, Guid pointTypeId)
        {
            return await (
               from h in Db.MsPointTypeMeasureItems
               where h.ProjectGuid == projectId && h.ProjectStandardId == projectStandardId && h.MsPointTypeId == pointTypeId
               select h).AsNoTracking().ToListAsync().ConfigureAwait(false);
        }
    }
}